package com.github.microwww.upms.controller;

import com.github.microwww.upms.service.UserService;
import com.github.microwww.upms.vo.UserValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping(value = "/user")
public class UserController {

    @Autowired()
    UserService userService;

    @GetMapping(value = "/detail")
    public UserValue.Info detailUser(int id) {
        return this.userService.findById(id).map(UserValue.Info::new).orElseGet(null);
    }

    @GetMapping(value = "/list")
    public List<UserValue.Simple> listUser(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        return this.userService.findAll(page, size).stream().map(UserValue.Simple::new).collect(Collectors.toList());
    }
}
